package com.ldh.jedis.resp;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @program: jedis1
 * @description:
 * @author: Donghui Li
 * @create: 2021-12-08 21:45
 */
public class ClientEndecode {

    private static final String SIMPLE_STRING = "+";

    private static final String BULK_STRING = "$";

    private static final String ERROR = "-";

    private static final String INTEGER = ":";

    private static final String ARRAY = "*";

    private static final String CTRL = "\r\n";

     public static String ArrayStringEncode(String command){
        StringBuilder stringBuilder = new StringBuilder();
        String[] s = command.split(" ");
        stringBuilder.append(ARRAY).append(s.length).append(CTRL);

        for(String str : s){
            stringBuilder.append(BULK_STRING).append(str.length()).append(CTRL);
            stringBuilder.append(str).append(CTRL);
        }
        return stringBuilder.toString();
    }

    public static String SimpleStringEncode(String command){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SIMPLE_STRING).append(command).append(CTRL);
        return stringBuilder.toString();
    }

    public static String ErrorEncode(String command){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(ERROR).append(command).append(CTRL);
        return stringBuilder.toString();
    }

    public static String IntegerEncode(String command){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(INTEGER).append(command).append(CTRL);
        return stringBuilder.toString();
    }

    public static String decode(BufferedReader bufferedReader) throws IOException {
        String info = null;
        char[] chars = new char[1];
        bufferedReader.read(chars,0,1);
        switch (chars[0]){
            case '*':
                info = ArrayDecode(bufferedReader);
                break;
            case '+':
                info = SimpleStringDecode(bufferedReader);
                break;
            case '-':
                info = ErrorDecode(bufferedReader);
                break;
            case ':':
                info = IntegerDecode(bufferedReader);
                break;
            case '$':
                info = BulkStringDecode(bufferedReader);
                break;
            default:
                info = ErrorEncode("command not found");
                break;
        }
        return info;
    }

    private static String ArrayDecode(BufferedReader bufferedReader) throws IOException {
        int len = Integer.valueOf(bufferedReader.readLine());
        StringBuilder ans = new StringBuilder();
        for(int q=0;q<len;q++){
            String sign = bufferedReader.readLine();
            int nlen = Integer.valueOf(sign.substring(1));
            char[] s = new char[nlen];
            bufferedReader.read(s);
            bufferedReader.readLine();
           ans.append(String.valueOf(s)).append(CTRL);
        }
        return ans.toString();
    }

    private static String SimpleStringDecode(BufferedReader bufferedReader) throws IOException {
        return bufferedReader.readLine();
    }

    private static String ErrorDecode(BufferedReader bufferedReader) throws IOException {
        return "(error)"+bufferedReader.readLine();
    }

    private static String IntegerDecode(BufferedReader bufferedReader) throws IOException {
        return "(integer)"+bufferedReader.readLine();
    }

    private static String BulkStringDecode(BufferedReader bufferedReader) throws IOException {
        bufferedReader.readLine();
        return bufferedReader.readLine();
    }

}
